##############################################################
### Replication Code for Hand-Coded Content Analysis       ###
### Title: Relative Gains in the Shadow of a Trade War     ###
### Authors: Eddy S. F. Yeung and Kai Quek                 ###
### Version: December 6, 2021                              ###
##############################################################

### Set-up ###
## Clean the R environment and set the working directory
# RStudio version: 1.3.959
rm(list = ls())
setwd("~/Downloads/trade_replication") # change to your own working directory

## Load the required packages
library(readxl)    # version 1.3.1
library(tidyverse) # version 1.3.1
library(extrafont) # version 0.17
# windowsFonts("Times" = windowsFont("Times")) # may be required for Windows users

## Import the dataset that contains the summary statistics of open-ended responses
df <- read_excel("summary_hand-coded.xlsx")

## Subset the dataset
df1 <- df %>% filter(Set == 1) %>% add_column(Lower_bound = NA, Upper_bound = NA)
df2 <- df %>% filter(Set == 2)

## Reorder factors
df1$Treatment <- 
  factor(df1$Treatment, levels = c("No Additional Vignette", "Additional Vignette"))
df1$Category <- 
  factor(df1$Category, levels = c("Other-Regarding", "Self-Serving", "Retaliation", "Other"))
df2$Treatment <- 
  factor(df2$Treatment, levels = c("No Additional Vignette", "Additional Vignette"))
df2$Category <- 
  factor(df2$Category, levels = c("Altruism/Empathy", "Equality/Fairness",
                                  "Cosmopolitanism", "Other"))

### Figure 4: Coding of Open-Ended Responses by Treatment Condition in the Win-Lose Scenario ###
## Compute the confidence intervals for each experimental group
for(i in 1:16) {
  temp <- prop.test(x = df1$Frequency[i], n = df1$Total[i],
                    correct = F)
  df1$Lower_bound[i] <- temp$conf.int[1] * 100
  df1$Upper_bound[i] <- temp$conf.int[2] * 100
}

### Analysis for the Win-Lose Scenario ###
## Test the statistical significance between the prop differences between groups
# Percentages of other-regarding responses with China as the trade partner
prop.test(x = c(df1$Frequency[1], df1$Frequency[5]), 
          n = c(df1$Total[1], df1$Total[5]),
          alternative = "two.sided", conf.level = .95, correct = F)
          # beta = 0.16, p = 0.0011, n = 388 (reported in the main text)

# Percentages of self-serving responses with China as the trade partner
prop.test(x = c(df1$Frequency[2], df1$Frequency[6]), 
          n = c(df1$Total[2], df1$Total[6]),
          alternative = "two.sided", conf.level = .95, correct = F)
          # beta = -0.16, p = 0.0017, n = 388 (reported in the main text)

# Percentages of other-regarding responses with Country X as the trade partner
prop.test(x = c(df1$Frequency[9], df1$Frequency[13]), 
          n = c(df1$Total[9], df1$Total[13]),
          alternative = "two.sided", conf.level = .95, correct = F)
          # beta = 0.13, p = 0.0111, n = 380 (reported in Footnote 80)

# Percentages of self-serving responses with Country X as the trade partner
prop.test(x = c(df1$Frequency[10], df1$Frequency[14]), 
          n = c(df1$Total[10], df1$Total[14]),
          alternative = "two.sided", conf.level = .95, correct = F)
          # beta = -0.16, p = 0.0016, n = 380 (reported in Footnote 80)

## Visualize the results for China
ggplot(data = subset(df1, Country %in% "China"), 
       aes(x = Category, y = Percentage, fill = Treatment)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
  scale_x_discrete(breaks = c("Other-Regarding", "Self-Serving",
                              "Retaliation", "Other")) +
  scale_fill_manual(values = c("grey90", "grey50")) +
  geom_errorbar(width = .2, aes(ymin = Lower_bound, ymax = Upper_bound), 
                position = position_dodge(.9)) +
  xlab("") + ylab("Percentage of Responses under Each Category") +
  theme_classic() +
  theme(text = element_text(family = "Times", size = 14)) +
  theme(axis.text = element_text(colour = "black", size = 14),
        legend.justification = c(1,1), legend.position = c(0.98,0.98),
        legend.box.background = element_rect(color = "black"), 
        legend.key.size = unit(1.5, "line")) +
  coord_cartesian(ylim = c(0,60))
ggsave("hand-coded_set1_China.pdf", width = 6, height = 6, dpi = 500)

## Visualize the results for Country X
ggplot(data = subset(df1, Country %in% "Country X"), 
       aes(x = Category, y = Percentage, fill = Treatment)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
  scale_x_discrete(breaks = c("Other-Regarding", "Self-Serving",
                              "Retaliation", "Other")) +
  scale_fill_manual(values = c("grey90", "grey50")) +
  geom_errorbar(width = .2, aes(ymin = Lower_bound, ymax = Upper_bound), 
                position = position_dodge(.9)) +
  xlab("") + ylab("Percentage of Responses under Each Category") +
  theme_classic() +
  theme(text = element_text(family = "Times", size = 14)) +
  theme(axis.text = element_text(colour = "black", size = 14),
        legend.justification = c(1,1), legend.position = c(0.98,0.98),
        legend.box.background = element_rect(color = "black"), 
        legend.key.size = unit(1.5, "line")) +
  coord_cartesian(ylim = c(0,60))
ggsave("hand-coded_set1_CntryX.pdf", width = 6, height = 6, dpi = 500)

### Figure 5: Disaggregation of Other-Regarding Responses by Treatment Condition in the Win-Lose Scenario ###
## Visualize the results for China
ggplot(data = subset(df2, Country %in% "China"), 
       aes(x = Category, y = Frequency, fill = Treatment)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
  scale_x_discrete(breaks = c("Altruism/Empathy", "Equality/Fairness",
                              "Cosmopolitanism", "Other")) +
  scale_fill_manual(values = c("grey90", "grey50")) +
  xlab("") + ylab("Number of Responses under Each Category") +
  theme_classic() +
  theme(text = element_text(family = "Times", size = 14)) +
  theme(axis.text = element_text(colour = "black", size = 12),
        legend.justification = c(1,1), legend.position = c(0.98,0.98),
        legend.box.background = element_rect(color = "black"), 
        legend.key.size = unit(1.5, "line")) +
  coord_cartesian(ylim = c(0,70))
ggsave("hand-coded_set2_China_alt.pdf", width = 6, height = 6, dpi = 500)

## Visualize the results for Country X
ggplot(data = subset(df2, Country %in% "Country X"), 
       aes(x = Category, y = Frequency, fill = Treatment)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
  scale_x_discrete(breaks = c("Altruism/Empathy", "Equality/Fairness",
                              "Cosmopolitanism", "Other")) +
  scale_fill_manual(values = c("grey90", "grey50")) +
  xlab("") + ylab("Number of Responses under Each Category") +
  theme_classic() +
  theme(text = element_text(family = "Times", size = 14)) +
  theme(axis.text = element_text(colour = "black", size = 12),
        legend.justification = c(1,1), legend.position = c(0.98,0.98),
        legend.box.background = element_rect(color = "black"), 
        legend.key.size = unit(1.5, "line")) +
  coord_cartesian(ylim = c(0,70))
ggsave("hand-coded_set2_CntryX_alt.pdf", width = 6, height = 6, dpi = 500)
